/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frm_JInterFrarmeHienThiSachTheoTheLoai.java
 *
 * Created on Aug 2, 2011, 6:03:27 PM
 */

package gui;


import dao.SachDAO;
import dao.TheLoaiDAO;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import pojo.SachPOJO;
import pojo.TheLoaiPOJO;

/**
 *
 * @author HP
 */
public class frm_JInterFrarmeHienThiSachTheoTheLoai extends javax.swing.JInternalFrame {

    /** Creates new form frm_JInterFrarmeHienThiSachTheoTheLoai */
    TheLoaiDAO theloaidao=new TheLoaiDAO();
    ArrayList<TheLoaiPOJO> dsTheloai=new  ArrayList<TheLoaiPOJO>();
    ArrayList<SachPOJO>dsSach=new ArrayList<SachPOJO>();
    SachDAO sachdao=new SachDAO();

    private DefaultTableModel tablemodelTheLoai;
        private DefaultTableModel tablemodelSach;
    private int stt;

    public frm_JInterFrarmeHienThiSachTheoTheLoai() {
        initComponents();
    this.tablemodelTheLoai =new DefaultTableModel();
    this.tablemodelTheLoai.addColumn("STT");
    this.tablemodelTheLoai.addColumn("Ma The Loai");
     this.tablemodelTheLoai.addColumn("Ten The Loai");
  jtbDSTheLoai.setModel(tablemodelTheLoai);
  
  tablemodelSach=new DefaultTableModel();
  this.tablemodelSach.addColumn("STT");
  this.tablemodelSach.addColumn("Ma Sach");
   this.tablemodelSach.addColumn("Ten Sach");
     this.tablemodelSach.addColumn("Gia Ban");
    this.jtbDanhSach.setModel(tablemodelSach);


     dsTheloai = theloaidao.layDanhSach_TheLoai();
         HienThiDanhSachTheLoai();
         InitComBoboxTheLoai();

    }

  private void InitComBoboxTheLoai() {
        try {
            DefaultComboBoxModel dfcbxTheLoai = new DefaultComboBoxModel();



            for (TheLoaiPOJO i : dsTheloai) {
                Vector vt = new Vector();
                vt.add(i.getMaTheLoai());
                vt.add(i.getTenTheLoai());
                //them mot dong voao model
                dfcbxTheLoai.addElement(vt);
            }
            this.jcbxTheLoai.setModel(dfcbxTheLoai);


        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage(), "Đã xảy ra lỗi", JOptionPane.ERROR_MESSAGE);
        }
    }


  public void ClearTableDSSach()
{

    javax.swing.table.DefaultTableModel mode=(DefaultTableModel) this.jtbDanhSach.getModel();
    int t=0;


    for (int i=jtbDanhSach.getRowCount()-1;i>=0;i--)
    {
         mode.removeRow(i);
//
    }
}
private void HienThiDanhSachTheLoai()
{
   // dsTheloai=theloaidao.layDanhSach_TheLoai();

  
     javax.swing.table.DefaultTableModel mode=(DefaultTableModel) this.jtbDSTheLoai.getModel();
     int t=1;
     for(TheLoaiPOJO i : dsTheloai )
     {
          mode.addRow(new Object[]{t,i.getMaTheLoai(),i.getTenTheLoai()});
          t++;
     }

}

private void HienThiDanhSachSachTheoTheLoai(int matheloai)
{
    dsSach=sachdao.layDSSach_TheoTheLoai(matheloai);
    
   ClearTableDSSach();
     javax.swing.table.DefaultTableModel mode=(DefaultTableModel) this.jtbDanhSach.getModel();
     int t=1;
     for(SachPOJO i : dsSach )
     {
          mode.addRow(new Object[]{t,i.getMaSach(),i.getTenSach(),i.getGiaBan()});
          t++;
     }

}

public  void cleareTable(JTable tablename)
{
    for (int i=0 ;i < tablename.getRowCount(); i++)
    {
        for(int j=0;j<tablename.getColumnCount();j++)
        {
            tablename.setValueAt((Object)"", i, j);
           
        }
    }
}
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtbDSTheLoai = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        jcbxTheLoai = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jtbDanhSach = new javax.swing.JTable();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Hien Thi Sach Theo San Pham");

        jPanel1.setBackground(new java.awt.Color(255, 153, 255));

        jPanel2.setBackground(new java.awt.Color(204, 204, 255));

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24));
        jLabel1.setText("Hiển Thị Sách Theo Thể Loại");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(236, 236, 236)
                .addComponent(jLabel1)
                .addContainerGap(504, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1)
        );

        jPanel3.setBackground(new java.awt.Color(204, 204, 255));
        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 204), 2), "Danh Sach The Loai"));

        jtbDSTheLoai.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jtbDSTheLoai.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jtbDSTheLoaiMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jtbDSTheLoai);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel2.setText("Thể Loại");

        jcbxTheLoai.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbxTheLoaiActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel3.setText("Danh Sách Thể Loại");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 370, Short.MAX_VALUE)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 57, Short.MAX_VALUE)
                        .addComponent(jcbxTheLoai, javax.swing.GroupLayout.PREFERRED_SIZE, 245, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel3)))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jcbxTheLoai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 358, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jPanel4.setBackground(new java.awt.Color(204, 204, 255));
        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 255), 2), "Danh Sach Sach/ The Loai"));

        jtbDanhSach.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane2.setViewportView(jtbDanhSach);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 642, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(19, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jtbDSTheLoaiMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtbDSTheLoaiMouseClicked
        // TODO add your handling code here:
        try

        {

            int vt=jtbDSTheLoai.getSelectedRow();

          int ma= Integer.parseInt(jtbDSTheLoai.getValueAt(vt, 1).toString().trim());

             HienThiDanhSachSachTheoTheLoai(ma);
        }
        catch(Exception ex)
        {
        }

    }//GEN-LAST:event_jtbDSTheLoaiMouseClicked

    private void jcbxTheLoaiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbxTheLoaiActionPerformed
        // TODO add your handling code here:
        try
        {
            Vector vttheloai = (Vector) this.jcbxTheLoai.getSelectedItem();
            int matheloai = (Integer) vttheloai.get(0);
             HienThiDanhSachSachTheoTheLoai(matheloai);
        }
        catch(Exception ex)
        {

        }
    }//GEN-LAST:event_jcbxTheLoaiActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JComboBox jcbxTheLoai;
    private javax.swing.JTable jtbDSTheLoai;
    private javax.swing.JTable jtbDanhSach;
    // End of variables declaration//GEN-END:variables

}
